from flask import request, jsonify, Blueprint
from app import LogService
from app.models import Station
from presenter import StationPresenter
from utils import create_response, StateCode

station_bp = Blueprint('stations', __name__)


@station_bp.route('/stations', methods=['POST'])
def createStation():  # 创建站点方法
    data = request.json
    new_station = Station.create(data=data)
    station_presenter = StationPresenter(new_station).as_dict()
    LogService.log()
    return jsonify(create_response(StateCode.SUCCESS, data=station_presenter)), 200


@station_bp.route('/stations/quantity_create', methods=['POST'])
def quantityCreate():  # 批量创建站点方法
    stations = request.json.get("stations")
    for name in stations:
        station_hash = {"name": name}
        Station.create(station_hash)
    LogService.log()
    return jsonify(create_response(StateCode.SUCCESS)), 200
